package de.jare.photoswapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


// http://en.androidwiki.com/wiki/Loading_images_from_a_remote_server
// erstmla nur _ein_ statisches bild...

public class Get extends Activity {
     
	 HttpHandler h = new HttpHandler();
     ImageView imView;
     String imageUrl="http://swap.ganzbunt.de/";
    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.get);
        Button bt3= (Button)findViewById(R.id.get_imagebt);
        bt3.setOnClickListener(getImgListener);
        h.AndroidID = System.getString(this.getContentResolver(), System.ANDROID_ID);
        imView = (ImageView)findViewById(R.id.imview);
    }    

    View.OnClickListener getImgListener = new View.OnClickListener()
    {
          @Override
          public void onClick(View view) {
               // TODO Auto-generated method stub
              // h.AndroidID = System.getString(this.getContentResolver(), System.ANDROID_ID);
        	  downloadFile(imageUrl+"get.php?AndroidId="+h.AndroidID);
              Log.i("im url ",imageUrl+"get.php?AndroidId="+h.AndroidID);
          }
    };

   
    Bitmap bmImg;
    void downloadFile(String fileUrl){
          URL myFileUrl =null;          
          try {
               myFileUrl= new URL(fileUrl);
          } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          try {
               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               //int length = conn.getContentLength();
               InputStream is = conn.getInputStream();
               
               bmImg = BitmapFactory.decodeStream(is);
               imView.setImageBitmap(bmImg);
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}